#-*-coding:utf-8-*-

#引入库
import pyaudio
import wave

# 定义数据流块
CHUNK = 1024

# if len(sys.argv) < 2:
#     print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
#     sys.exit(-1)

# 只读方式打开wav文件
wf = wave.open(r'record.wav', 'rb')#(sys.argv[1], 'rb')

p = pyaudio.PyAudio()
print(wf.getsampwidth())  # =2 2个字节 16bit表示一个采样，强度在0~2^16
print(wf.getnchannels()) 
print(wf.getframerate())
# 10s的音频，data的总长度为882000=44100*2*10

# 打开数据流
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

# 读取数据
data = wf.readframes(CHUNK)
dLength = 0
# 播放  
while len(data)>0:
    dLength += len(data)
    stream.write(data)
    data = wf.readframes(CHUNK)

# 停止数据流  
stream.stop_stream()
stream.close()
wf.close()
# 关闭 PyAudio  
p.terminate()
print(dLength)
